﻿using System.Web.Mvc;
using System.Web.Security;
using ProjectFootball.MVC.Areas.AccountArea.Controllers.Foundation;
using ProjectFootball.MVC.Areas.AccountArea.Models;
using WebMatrix.WebData;

namespace ProjectFootball.MVC.Areas.AccountArea.Controllers
{
    public class RegisterController : BaseAccountController
    {
        //
        // GET: /Account/Register

        [AllowAnonymous]
        public ActionResult Index()
        {
            return View();
        }

        //
        // POST: /Account/Register

        [HttpPost]
        [AllowAnonymous]
        [ValidateAntiForgeryToken]
        public ActionResult Index(RegisterModel model)
        {
            if (ModelState.IsValid)
            {
                // Attempt to register the user
                try
                {
                    WebSecurity.CreateUserAndAccount(model.UserName, model.Password);
                    WebSecurity.Login(model.UserName, model.Password);
                    return RedirectToAction("Index", "Home", new { area = "" });
                }
                catch (MembershipCreateUserException e)
                {
                    ModelState.AddModelError("", ErrorCodeToString(e.StatusCode));
                }
            }

            // If we got this far, something failed, redisplay form
            return View(model);
        }

    }
}
